﻿
Imports lock_t = lock(Of locks.simplelock)

Namespace std
    Public Class keyvalueless(Of keyT As IComparable(Of keyT), valueT, implT As keyvalue(Of keyT, valueT))
        Private ReadOnly kv As implT = Nothing
        Private l As lock_t = Nothing

        Public Sub New(ByVal kv As implT)
            If kv Is Nothing Then
                Me.kv = alloc(Of implT)()
            Else
                Me.kv = kv
            End If
        End Sub

        Public Sub New()
            Me.New(Nothing)
        End Sub

        Public Function write(ByVal key As keyT, ByVal value As valueT) As Boolean
            Return l.locked(Function() kv.write(key, value))
        End Function

        Public Function uniqueWrite(ByVal key As keyT, ByVal value As valueT) As Boolean
            Return l.locked(Function() kv.uniqueWrite(key, value))
        End Function

        Public Function delete(ByVal key As keyT) As Boolean
            Return l.locked(Function() kv.delete(key))
        End Function

        Public Function modify(ByVal key As keyT, ByVal value As valueT) As Boolean
            Return l.locked(Function() kv.modify(key, value))
        End Function

        Public Function read(ByVal key As keyT, ByRef value As valueT) As Boolean
            Dim v As valueT = Nothing
            Return l.locked(Function() kv.read(key, v)) AndAlso eva(value, v)
        End Function

        Public Function seek(ByVal key As keyT) As Boolean
            Return l.locked(Function() kv.seek(key))
        End Function

        Public Function capacity() As Int64
            Return l.locked(Function() kv.capacity())
        End Function

        Public Function valuesize() As Int64
            Return l.locked(Function() kv.valuesize())
        End Function

        Public Function list(ByRef rtn As vector(Of keyT)) As Boolean
            Dim v As vector(Of keyT) = Nothing
            Return l.locked(Function() kv.list(v)) AndAlso eva(rtn, v)
        End Function

        Public Function status(Optional ByRef health_slices As Int64 = Nothing,
                               Optional ByRef all_slices As Int64 = Nothing) As Boolean
            Dim h As Int64 = 0
            Dim a As Int64 = 0
            Return l.locked(Function() kv.status(h, a)) AndAlso eva(health_slices, h) AndAlso eva(all_slices, a)
        End Function

        Public Function foreach(ByVal d As _do(Of keyT, valueT, Boolean, Boolean)) As Boolean
            Return l.locked(Function() kv.foreach(d))
        End Function

        Public Function open() As Boolean
            Return l.locked(Function() kv.open())
        End Function

        Public Function close() As Boolean
            Return l.locked(Function() kv.close())
        End Function

        Public Function full() As Boolean
            Return l.locked(Function() kv.full())
        End Function

        Public Function health() As Boolean
            Return l.locked(Function() kv.health())
        End Function

        Public Function retire() As Boolean
            Return l.locked(Function() kv.retire())
        End Function

        Public Function empty() As Boolean
            Return l.locked(Function() kv.empty())
        End Function

        Public Function sizeof(ByVal key As keyT, ByRef sz As Int64) As Boolean
            Dim v As Int64 = 0
            Return l.locked(Function() kv.sizeof(key, v)) AndAlso eva(sz, v)
        End Function

        Public Function keycount(ByRef c As Int64) As Boolean
            Dim v As Int64 = 0
            Return l.locked(Function() kv.keycount(v)) AndAlso eva(c, v)
        End Function

        Public Function keycount() As Int64
            Return l.locked(Function() kv.keycount())
        End Function

        Public Function sync(ByVal host As String, ByVal port As Int32) As Boolean
            Return l.locked(Function() kv.sync(host, port))
        End Function

        Public Function version() As String
            Return l.locked(Function() kv.version())
        End Function

        Public Function lockwrite() As Boolean
            Return l.locked(Function() kv.lockwrite())
        End Function

        Public Function unlockwrite() As Boolean
            Return l.locked(Function() kv.unlockwrite())
        End Function

        Public Function lockread() As Boolean
            Return l.locked(Function() kv.lockread())
        End Function

        Public Function unlockread() As Boolean
            Return l.locked(Function() kv.unlockread())
        End Function

        Public Function lock() As Boolean
            Return l.locked(Function() kv.lock())
        End Function

        Public Function unlock() As Boolean
            Return l.locked(Function() kv.unlock())
        End Function
    End Class

    Public Class keyvalueless(Of keyT As IComparable(Of keyT), valueT)
        Inherits keyvalueless(Of keyT, valueT, keyvalue(Of keyT, valueT))

        Public Sub New(ByVal kv As keyvalue(Of keyT, valueT))
            MyBase.New(kv)
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub
    End Class

    Public Class keybytesless(Of keyT As IComparable(Of keyT))
        Inherits keyvalueless(Of keyT, bytes, keybytes(Of keyT))

        Public Sub New(ByVal kv As keybytes(Of keyT))
            MyBase.New(kv)
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub
    End Class
End Namespace
